/*
 * Created on 10/12/2012 - 3:30 PM
 */
package dxfpresentation;

import java.util.ArrayList;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import sembr.postprocessing.SembrPostprocessing;
import sembr.vtk.animation.VtkMovie3D;
import sembr.vtk.reader.PngReader;
import sembr.vtk.reader.ReaderBase;

/**
 *
 * @author adriano
 */
public class PostProcessingConfig extends javax.swing.JDialog {
    /**
     * Creates new form PostProcessingConfig
     */
    public PostProcessingConfig(java.awt.Frame parent, boolean modal) {
        
        super(parent, modal);
        initComponents();
        
        rdMovie2D.setVisible(false);
        rdMovie3D.setVisible(false);
        rdField.setVisible(false);
        pnlMovieSettings.setVisible(false);
        progressBarPostProcessing.setVisible(false);
        lblPostProcessingStatus.setVisible(false);
        lblTimeStep.setVisible(false);
        txtTimeStep.setVisible(false);
        isGeneratingPostProcessing = false;
    }
    
    public void setPostProcessing(String projectPath, String fieldComponent)
    {
        this.setProjectPath(projectPath);
        this.setFieldComponent(fieldComponent);
        postprocessing = new SembrPostprocessing(this.getProjectPath(), this.getFieldComponent());
    }
    
    public void setPostProcessingReport(boolean isMovie2D, boolean isMovie3D, boolean isField) {
        
        if (isMovie2D && isMovie3D && isField)
        {
            rdMovie2D.setVisible(isMovie2D);
            rdMovie3D.setVisible(isMovie3D);
            rdField.setVisible(isField);
            // if the three radio buttons are choosed, set 2D movie checked as default report
            rdMovie2D.setSelected(isMovie2D);            
        }
        else if (isMovie2D && isMovie3D)
        {
            rdMovie2D.setVisible(isMovie2D);
            rdMovie3D.setVisible(isMovie3D);
            rdMovie2D.setSelected(isMovie2D);
        }
        else if (isMovie2D && isField)
        {
            rdMovie2D.setVisible(isMovie2D);
            rdField.setVisible(isField);
            rdMovie2D.setSelected(isMovie2D);
        }
        else if (isMovie2D)
        {
            rdMovie2D.setVisible(isMovie2D);
            rdMovie2D.setSelected(isMovie2D);
            lblTimeStep.setVisible(isMovie2D);
            txtTimeStep.setVisible(isMovie2D);
        }
        else if (isMovie3D && isField)
        {
            rdMovie3D.setVisible(isMovie3D);
            rdField.setVisible(isField);
            rdMovie3D.setSelected(isMovie3D);
        }
        
        else if (isMovie3D)
        {
            rdMovie3D.setVisible(isMovie3D);
            rdMovie3D.setSelected(isMovie3D);
            lblTimeStep.setVisible(isMovie3D);
            txtTimeStep.setVisible(isMovie3D);
        }
        else
        {
            rdField.setVisible(isField);
            rdField.setSelected(isField);
        }
    }
   
    public void setObjectBoundsInHDF5(int[] objectBounds) {
        
        objectBoundsInHDF5 = objectBounds;
    }
    
    public int[] getObjectBoundForHDF5() {
        
        return objectBoundsInHDF5;
    }
    
    /**
     * @return the volumeExtention
     */
    public int[] getVolumeExtention() {
        
        return volumeExtention;
    }

    /**
     * @param volumeExtention the volumeExtention to set
     */
    public void setVolumeExtention(int[] volumeExtention) {
        
        this.volumeExtention = volumeExtention;
    }
    
    public void populateFormFields() {    
        
        txtPostProcessingGeometryDimension.setText(postprocessing.getGeometryDimension());
        txtPostProcessingSimulationDimension.setText(postprocessing.getSimulationDimension());
        txtTimeStep.setHorizontalAlignment(JTextField.RIGHT);
        lblTimeStep.setHorizontalAlignment(JTextField.RIGHT);
        setSlicesByCutAxis(cmbCutAxis.getSelectedIndex(), postprocessing.getSimulationDimension());   
        
        if (rdMovie2D.isSelected())
        {
            txtInitialTime.setText("0");
            pnlMovieSettings.setVisible(true);
        }
        else if (rdMovie3D.isSelected())
        {
            txtInitialTime.setText(String.valueOf(dimensions[3] / 2));
            pnlMovieSettings.setVisible(true);
        }
        else if (rdField.isSelected())
        {
            txtInitialTime.setText(String.valueOf(dimensions[3] - 1));
            pnlMovieSettings.setVisible(false);
        }
        else
        {
            txtInitialTime.setText("0");
        }
        
        txtEndTime.setText(String.valueOf(dimensions[3] - 1));
    }
    
    /**
     * @return the polydata
     */
    public ReaderBase getReaderBase() {
        
        return reader;
    }

    /**
     * @param polydata the polydata to set
     */
    public void setReaderBase(ReaderBase reader) {
        
        this.reader = reader;
    }
    
    /**
     * @return the isGeneratingPostProcessing
     */
    public boolean isGeneratingPostProcessing() {
       
        return isGeneratingPostProcessing;
    }

    /**
     * @param isGeneratingPostProcessing the isGeneratingPostProcessing to set
     */
    public void setIsGeneratingPostProcessing(boolean isGeneratingPostProcessing) {
        
        this.isGeneratingPostProcessing = isGeneratingPostProcessing;
    }
    
    /**
     * @return the movie3D
     */
    public VtkMovie3D getMovie3D() {
        
        return movie3D;
    }

    /**
     * @param movie3D the movie3D to set
     */
    public void setMovie3D(VtkMovie3D movie3D) {
        
        this.movie3D = movie3D;
    }
    
    /**
     * @return the projectPath
     */
    public String getProjectPath() {
        
        return projectPath;
    }

    /**
     * @param projectPath the projectPath to set
     */
    public void setProjectPath(String projectPath) {
        
        this.projectPath = projectPath;
    }

    /**
     * @return the fieldComponent
     */
    public String getFieldComponent() {
        
        return fieldComponent;
    }

    /**
     * @param fieldComponent the fieldComponent to set
     */
    public void setFieldComponent(String fieldComponent) {
        
        this.fieldComponent = fieldComponent;
    }
    
    /**
     * @return the movieName
     */
    public String getMovieName() {
        
        return movieName;
    }

    /**
     * @param movieName the movieName to set
     */
    public void setMovieName(String movieName) {
        
        this.movieName = movieName;
    }
    
    /**
     * @return the postProcessingAxesDirection
     */
    public String getPostProcessingAxesDirection() {
        
        return postProcessingAxesDirection;
    }

    /**
     * @param postProcessingAxesDirection the postProcessingAxesDirection to set
     */
    public void setPostProcessingAxisDirection(String postProcessingAxesDirection) {
        
        this.postProcessingAxesDirection = postProcessingAxesDirection;
    }   
    
    /**
     * @return the imageFileNames
     */
    public ArrayList<String> getImageFileNames() {
        
        return imageFileNames;
    }

    /**
     * @param imageFileNames the imageFileNames to set
     */
    public void setImageFileNames(ArrayList<String> imageFileNames) {
        
        this.imageFileNames = imageFileNames;
    }
    
    /**
     * @return the fieldRange
     */
    public double[] getFieldRange() {
        
        return fieldRange;
    }

    /**
     * @param fieldRange the fieldRange to set
     */
    public void setFieldRange(double[] fieldRange) {
        
        this.fieldRange = fieldRange;
    }
            
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPostProcessingConfig = new javax.swing.JPanel();
        jPanel12 = new javax.swing.JPanel();
        txtPostProcessingGeometryDimension = new javax.swing.JTextField();
        jPanel13 = new javax.swing.JPanel();
        txtPostProcessingSimulationDimension = new javax.swing.JTextField();
        jPanel14 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        cmbCutAxis = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtInitialTime = new javax.swing.JTextField();
        txtEndTime = new javax.swing.JTextField();
        txtInitialSlice = new javax.swing.JTextField();
        txtEndSlice = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        rdMovie2D = new javax.swing.JRadioButton();
        rdField = new javax.swing.JRadioButton();
        rdMovie3D = new javax.swing.JRadioButton();
        txtTimeStep = new javax.swing.JTextField();
        lblTimeStep = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        pnlMovieSettings = new javax.swing.JPanel();
        lblMovieWidth = new javax.swing.JLabel();
        lblMovieHeight = new javax.swing.JLabel();
        txtMovieWidth = new javax.swing.JTextField();
        txtMovieHeight = new javax.swing.JTextField();
        lblMovieFps = new javax.swing.JLabel();
        txtMovieFps = new javax.swing.JTextField();
        lblMovieName = new javax.swing.JLabel();
        txtMovieName = new javax.swing.JTextField();
        btnGeneratePostProcessing = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        progressBarPostProcessing = new javax.swing.JProgressBar();
        lblPostProcessingStatus = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Configuração de Pós-Processamento");

        pnlPostProcessingConfig.setBorder(javax.swing.BorderFactory.createTitledBorder("Configuração para visualização do Pós-Processamento"));
        pnlPostProcessingConfig.setRequestFocusEnabled(false);

        jPanel12.setBorder(javax.swing.BorderFactory.createTitledBorder("Propriedades de Geometria"));

        txtPostProcessingGeometryDimension.setEditable(false);
        txtPostProcessingGeometryDimension.setText("jTextField1");

        javax.swing.GroupLayout jPanel12Layout = new javax.swing.GroupLayout(jPanel12);
        jPanel12.setLayout(jPanel12Layout);
        jPanel12Layout.setHorizontalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel12Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtPostProcessingGeometryDimension, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel12Layout.setVerticalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel12Layout.createSequentialGroup()
                .addComponent(txtPostProcessingGeometryDimension, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel13.setBorder(javax.swing.BorderFactory.createTitledBorder("Propriedades de Simulação"));

        txtPostProcessingSimulationDimension.setEditable(false);
        txtPostProcessingSimulationDimension.setText("jTextField2");

        jPanel14.setBorder(javax.swing.BorderFactory.createTitledBorder("Corte"));

        jLabel3.setText("Eixo:");

        cmbCutAxis.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "x", "y", "z" }));
        cmbCutAxis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbCutAxisActionPerformed(evt);
            }
        });

        jLabel4.setText("Fatia:");

        jLabel1.setText("Tempo Inicial:");

        jLabel2.setText("Tempo Final:");

        txtInitialTime.setText("jTextField1");

        txtEndTime.setText("jTextField2");

        txtInitialSlice.setText("jTextField1");

        txtEndSlice.setText("jTextField2");

        javax.swing.GroupLayout jPanel14Layout = new javax.swing.GroupLayout(jPanel14);
        jPanel14.setLayout(jPanel14Layout);
        jPanel14Layout.setHorizontalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel14Layout.createSequentialGroup()
                        .addComponent(txtInitialSlice, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtEndSlice, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cmbCutAxis, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtInitialTime, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtEndTime, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        jPanel14Layout.setVerticalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cmbCutAxis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(txtInitialTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(txtInitialSlice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtEndSlice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtEndTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Relatórios de Pós-Processamento"));

        rdMovie2D.setText("Filme 2D");
        rdMovie2D.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdMovie2DActionPerformed(evt);
            }
        });

        rdField.setText("Distribuição de Campo em determinado tempo");
        rdField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdFieldActionPerformed(evt);
            }
        });

        rdMovie3D.setText("Filme 3D");
        rdMovie3D.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdMovie3DActionPerformed(evt);
            }
        });

        txtTimeStep.setText("1");

        lblTimeStep.setText("Passo de Tempo:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rdMovie2D)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(rdMovie3D, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(rdField))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblTimeStep)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTimeStep, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rdMovie2D)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdMovie3D)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdField)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 10, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTimeStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTimeStep)))
        );

        pnlMovieSettings.setBorder(javax.swing.BorderFactory.createTitledBorder("Configurações de Filme"));

        lblMovieWidth.setText("Largura:");

        lblMovieHeight.setText("Altura:");

        txtMovieWidth.setText("0");

        txtMovieHeight.setText("0");

        lblMovieFps.setText("Frames/Segundo:");

        txtMovieFps.setText("0");

        lblMovieName.setText("Nome do Arquivo:");

        txtMovieName.setText("video");

        javax.swing.GroupLayout pnlMovieSettingsLayout = new javax.swing.GroupLayout(pnlMovieSettings);
        pnlMovieSettings.setLayout(pnlMovieSettingsLayout);
        pnlMovieSettingsLayout.setHorizontalGroup(
            pnlMovieSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMovieSettingsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMovieSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMovieHeight)
                    .addComponent(lblMovieWidth))
                .addGap(18, 18, 18)
                .addGroup(pnlMovieSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtMovieWidth, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                    .addComponent(txtMovieHeight))
                .addGap(18, 18, 18)
                .addGroup(pnlMovieSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMovieName)
                    .addComponent(lblMovieFps))
                .addGap(3, 3, 3)
                .addGroup(pnlMovieSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtMovieFps, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMovieName, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pnlMovieSettingsLayout.setVerticalGroup(
            pnlMovieSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMovieSettingsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMovieSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMovieWidth)
                    .addComponent(txtMovieWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblMovieFps)
                    .addComponent(txtMovieFps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlMovieSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMovieHeight)
                    .addComponent(txtMovieHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblMovieName)
                    .addComponent(txtMovieName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel13Layout = new javax.swing.GroupLayout(jPanel13);
        jPanel13.setLayout(jPanel13Layout);
        jPanel13Layout.setHorizontalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtPostProcessingSimulationDimension, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlMovieSettings, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel13Layout.setVerticalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createSequentialGroup()
                .addComponent(txtPostProcessingSimulationDimension, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlMovieSettings, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        btnGeneratePostProcessing.setText("Gerar");
        btnGeneratePostProcessing.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGeneratePostProcessingActionPerformed(evt);
            }
        });

        btnClose.setText("Cancelar");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        lblPostProcessingStatus.setText("jLabel5");

        javax.swing.GroupLayout pnlPostProcessingConfigLayout = new javax.swing.GroupLayout(pnlPostProcessingConfig);
        pnlPostProcessingConfig.setLayout(pnlPostProcessingConfigLayout);
        pnlPostProcessingConfigLayout.setHorizontalGroup(
            pnlPostProcessingConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPostProcessingConfigLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPostProcessingConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlPostProcessingConfigLayout.createSequentialGroup()
                        .addComponent(btnClose)
                        .addGap(18, 18, 18)
                        .addComponent(progressBarPostProcessing, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblPostProcessingStatus)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnGeneratePostProcessing, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlPostProcessingConfigLayout.createSequentialGroup()
                        .addComponent(jPanel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlPostProcessingConfigLayout.setVerticalGroup(
            pnlPostProcessingConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlPostProcessingConfigLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlPostProcessingConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlPostProcessingConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnClose)
                        .addComponent(btnGeneratePostProcessing))
                    .addComponent(progressBarPostProcessing, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPostProcessingStatus))
                .addGap(12, 12, 12))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlPostProcessingConfig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlPostProcessingConfig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmbCutAxisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbCutAxisActionPerformed
        
        setSlicesByCutAxis(cmbCutAxis.getSelectedIndex(), null);
    }//GEN-LAST:event_cmbCutAxisActionPerformed

    private void btnGeneratePostProcessingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGeneratePostProcessingActionPerformed
        
        BackgroundPostProcessingTask postProcessingTask = new BackgroundPostProcessingTask();
        postProcessingTask.execute(); 
        
        progressBarPostProcessing.setVisible(true);
        progressBarPostProcessing.setIndeterminate(true);      
        
        //this.dispose();      
    }//GEN-LAST:event_btnGeneratePostProcessingActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed

        setIsGeneratingPostProcessing(false);
        this.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    private void rdMovie2DActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdMovie2DActionPerformed
        
        rdMovie2D.setSelected(true);
        rdMovie3D.setSelected(false);
        rdField.setSelected(false);
        
        setSlicesByCutAxis(cmbCutAxis.getSelectedIndex(), postprocessing.getSimulationDimension());
        txtInitialTime.setText("0");
        pnlMovieSettings.setVisible(true);
    }//GEN-LAST:event_rdMovie2DActionPerformed

    private void rdFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdFieldActionPerformed
        
        rdField.setSelected(true);
        rdMovie2D.setSelected(false);
        rdMovie3D.setSelected(false);
        
        setSlicesByCutAxis(cmbCutAxis.getSelectedIndex(), null);
        txtInitialTime.setText(String.valueOf(dimensions[3] - 1));
        pnlMovieSettings.setVisible(false);
    }//GEN-LAST:event_rdFieldActionPerformed

    private void rdMovie3DActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdMovie3DActionPerformed
        
        rdMovie3D.setSelected(true);
        rdField.setSelected(false);
        rdMovie2D.setSelected(false);
                
        setSlicesByCutAxis(cmbCutAxis.getSelectedIndex(), null);
        txtInitialTime.setText(String.valueOf(dimensions[3] - 1));
        pnlMovieSettings.setVisible(true);
    }//GEN-LAST:event_rdMovie3DActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PostProcessingConfig.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PostProcessingConfig.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PostProcessingConfig.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PostProcessingConfig.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                PostProcessingConfig dialog = new PostProcessingConfig(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }               

    private void setSlicesByCutAxis(int index, String value) {
        
        splitSimulationDimension(value);
        
        if (index == 0) // x axis
        {
            if (rdMovie2D.isSelected())
            {
                // 2d simulation. Z = 0
                // user don't modify slices, just time dimension
                if (dimensions[2] == 0)
                {
                    txtInitialSlice.setText("0");
                    txtInitialSlice.setEnabled(false);
                    txtEndSlice.setText("0"); 
                    txtEndSlice.setEnabled(false);
                    cmbCutAxis.setEnabled(false);
                }
                // 3d simulation - the user can modify slices for cutting
                else
                {
                   txtInitialSlice.setText(String.valueOf(dimensions[0] / 2));
                   txtEndSlice.setText(String.valueOf(dimensions[0] / 2));   
                } 
            }
            else if (rdField.isSelected())
            {
                txtInitialSlice.setText(String.valueOf("0"));
                txtEndSlice.setText(String.valueOf(dimensions[0] - 1)); 
            }
            else if (rdMovie3D.isSelected())
            {
                txtInitialSlice.setText(String.valueOf(objectBoundsInHDF5[0]));
                txtEndSlice.setText(String.valueOf(objectBoundsInHDF5[1]));  
            }
            else
            {
                txtInitialSlice.setText(String.valueOf(dimensions[0] - 1));
                txtEndSlice.setText(String.valueOf(dimensions[0] - 1));  
            }         
        }
        else if (index == 1) // y axis
        {
            if (rdMovie2D.isSelected())
            {
                txtInitialSlice.setText(String.valueOf(dimensions[1] / 2));
                txtEndSlice.setText(String.valueOf(dimensions[1] / 2)); 
            }
            else if (rdField.isSelected())
            {
                txtInitialSlice.setText(String.valueOf("0"));
                txtEndSlice.setText(String.valueOf(dimensions[1] - 1)); 
            }
            else if (rdMovie3D.isSelected())
            {
                txtInitialSlice.setText(String.valueOf(objectBoundsInHDF5[2]));
                txtEndSlice.setText(String.valueOf(objectBoundsInHDF5[3])); 
            }
            else
            {
                txtInitialSlice.setText(String.valueOf(dimensions[1] - 1));
                txtEndSlice.setText(String.valueOf(dimensions[1] - 1)); 
            }
        }
        else // z axis
        {
            if (rdMovie2D.isSelected())
            {
                 txtInitialSlice.setText(String.valueOf(dimensions[2] / 2));
                 txtEndSlice.setText(String.valueOf(dimensions[2] / 2));
            }
            else if (rdField.isSelected())
            {
                txtInitialSlice.setText(String.valueOf("0"));
                txtEndSlice.setText(String.valueOf(dimensions[2] - 1)); 
            }
            else if (rdMovie3D.isSelected())
            {
                txtInitialSlice.setText(String.valueOf(objectBoundsInHDF5[4]));
                txtEndSlice.setText(String.valueOf(objectBoundsInHDF5[5]));
            }
            else
            {
                txtInitialSlice.setText(String.valueOf(dimensions[2] - 1));
                txtEndSlice.setText(String.valueOf(dimensions[2] - 1)); 
            }
         }        
    }
    
    private void splitSimulationDimension(String properties) {
        
        if (properties == null) return;
        String delimiter = " ";
        String[] slicedProperties;
        ArrayList<String> simulationProperties = new ArrayList<String>();
        
        slicedProperties = properties.split(delimiter);
        // add sub-string related with field
        simulationProperties.add(slicedProperties[0]);             
        
        // beginning of 'Dataset' sub-string
        int i = 23;
        
        while (i <= (slicedProperties.length - 1))
        {
            simulationProperties.add(slicedProperties[i]);
            i++;
        }
        
        int xLength, yLength, zLength, timeLength;
        String x, y, z, time;
        
        int lastIndex = simulationProperties.size() - 1;
          
        switch (simulationProperties.size())
        {
            case 5: // bidimensional
                xLength = simulationProperties.get(lastIndex - 2).length();
                yLength = simulationProperties.get(lastIndex - 1).length();
                timeLength = simulationProperties.get(lastIndex).length();
        
                x = simulationProperties.get(lastIndex - 2).substring(1, xLength - 1);
                y = simulationProperties.get(lastIndex - 1).substring(0, yLength - 1);
                z = "0";
                time = simulationProperties.get(lastIndex).substring(0, timeLength - 5);
                break;
                
            case 6: // tridimensional
                xLength = simulationProperties.get(lastIndex - 3).length();
                yLength = simulationProperties.get(lastIndex - 2).length();
                zLength = simulationProperties.get(lastIndex - 1).length();
                timeLength = simulationProperties.get(lastIndex).length();
        
                x = simulationProperties.get(lastIndex - 3).substring(1, xLength - 1);
                y = simulationProperties.get(lastIndex - 2).substring(0, yLength - 1);
                z = simulationProperties.get(lastIndex - 1).substring(0, zLength - 1);
                time = simulationProperties.get(lastIndex).substring(0, timeLength - 5);    
                break;
            
            default:
                x = y = z = time = "0";
        }
        
        dimensions = new int[4];
        dimensions[0] = Integer.valueOf(x);
        dimensions[1] = Integer.valueOf(y); 
        dimensions[2] = Integer.valueOf(z);
        dimensions[3] = Integer.valueOf(time);
    }
    
    private int[] dimensions;
    private int[] volumeExtention;
    private int[] objectBoundsInHDF5;
    private SembrPostprocessing postprocessing;
    private ReaderBase reader;
    private VtkMovie3D movie3D;
    private boolean isGeneratingPostProcessing;
    private String movieName;
    private String projectPath;
    private String fieldComponent;
    private String postProcessingAxesDirection;
    private ArrayList<String> imageFileNames;
    private double[] fieldRange;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnGeneratePostProcessing;
    private javax.swing.JComboBox cmbCutAxis;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblMovieFps;
    private javax.swing.JLabel lblMovieHeight;
    private javax.swing.JLabel lblMovieName;
    private javax.swing.JLabel lblMovieWidth;
    private javax.swing.JLabel lblPostProcessingStatus;
    private javax.swing.JLabel lblTimeStep;
    private javax.swing.JPanel pnlMovieSettings;
    private javax.swing.JPanel pnlPostProcessingConfig;
    private javax.swing.JProgressBar progressBarPostProcessing;
    private javax.swing.JRadioButton rdField;
    private javax.swing.JRadioButton rdMovie2D;
    private javax.swing.JRadioButton rdMovie3D;
    private javax.swing.JTextField txtEndSlice;
    private javax.swing.JTextField txtEndTime;
    private javax.swing.JTextField txtInitialSlice;
    private javax.swing.JTextField txtInitialTime;
    private javax.swing.JTextField txtMovieFps;
    private javax.swing.JTextField txtMovieHeight;
    private javax.swing.JTextField txtMovieName;
    private javax.swing.JTextField txtMovieWidth;
    private javax.swing.JTextField txtPostProcessingGeometryDimension;
    private javax.swing.JTextField txtPostProcessingSimulationDimension;
    private javax.swing.JTextField txtTimeStep;
    // End of variables declaration//GEN-END:variables

    private class BackgroundPostProcessingTask extends SwingWorker<Object, Object> {
                
        @Override
        protected Object doInBackground() throws Exception 
        {
            lblPostProcessingStatus.setText("Gerando imagens...");
            lblPostProcessingStatus.setVisible(true);
            
            SembrPostprocessing postprocessing = new SembrPostprocessing(getProjectPath(), getFieldComponent(), getObjectBoundForHDF5());
            
            if (rdField.isSelected())
            {                
                postprocessing.generateImagesForField(cmbCutAxis.getSelectedItem().toString(), 
                                                      txtInitialSlice.getText(), 
                                                      txtEndSlice.getText(),
                                                      txtInitialTime.getText(), 
                                                      txtEndTime.getText(),
                                                      txtTimeStep.getText());
                
                lblPostProcessingStatus.setText("Gerando volume...");
                setReaderBase(new PngReader(postprocessing.getFieldComponent(),
                                            postprocessing.getFileNames(),
                                            postprocessing.getFieldRange(),
                                            false)); 
                
                setPostProcessingAxisDirection(cmbCutAxis.getSelectedItem().toString());
                setImageFileNames(postprocessing.getFileNamesAsJavaArray());
                // set field range
                setFieldRange(postprocessing.getFieldRange());
            }
            else if (rdMovie2D.isSelected())
            {
                postprocessing.generateImagesForField("t", 
                                                      txtInitialSlice.getText(), 
                                                      txtEndSlice.getText(),
                                                      txtInitialTime.getText(), 
                                                      txtEndTime.getText(),
                                                      txtTimeStep.getText());
                
                lblPostProcessingStatus.setText("Gerando vídeo...");
                postprocessing.generateMovie2D(txtMovieWidth.getText(), 
                                               txtMovieHeight.getText(), 
                                               txtMovieFps.getText(), 
                    
                                               txtMovieName.getText());
                
                setMovieName(txtMovieName.getText());
            }
            else if (rdMovie3D.isSelected())
            {
                postprocessing.generateImagesForField(cmbCutAxis.getSelectedItem().toString(), 
                                                      txtInitialSlice.getText(), 
                                                      txtEndSlice.getText(),
                                                      txtInitialTime.getText(), 
                                                      txtEndTime.getText(),
                                                      txtTimeStep.getText());
                
                lblPostProcessingStatus.setText("Gerando animação...");
                int nrOfFrames = ((Integer.valueOf(txtEndTime.getText()) - Integer.valueOf(txtInitialTime.getText())) + 1) / Integer.valueOf(txtTimeStep.getText());
                setMovie3D(new VtkMovie3D(getProjectPath(),
                                          cmbCutAxis.getSelectedItem().toString(),
                                          nrOfFrames,
                                          Integer.valueOf(txtInitialTime.getText()),
                                          Integer.valueOf(txtTimeStep.getText()),
                                          null,
                                          null,
                                          getFieldComponent()
                                         )
                          );
            }
            
            setIsGeneratingPostProcessing(true);
               
            return null;
        } 
        
        @Override
        protected void done()
        {
            progressBarPostProcessing.setIndeterminate(false);
            lblPostProcessingStatus.setText("Finalizado!");
        }
    }
}
